//
//  XJAddressCell.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/22.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJAddressCell.h"
@interface XJAddressCell()
@property(nonatomic, strong) UIView *bgView; // 背景view
@property(nonatomic, strong) UILabel *nameLabel; // 1.姓名
@property(nonatomic, strong) UILabel *phoneLabel;// 2.手机号
@property(nonatomic, strong) UILabel *addressLabel;//3.收货地址

// UIButton 默认左image 右 title(默认白色，xib的是蓝色)
@property(nonatomic, strong) UIButton *selectButton; // 4.选择button
@property(nonatomic, strong) UIButton *editButton; //5.编辑按钮
@property(nonatomic, strong) UIButton *deleteButton; // 6.删除按钮


@end

@implementation XJAddressCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark - 一 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.nameLabel];
    [self.bgView addSubview:self.phoneLabel];
    [self.bgView addSubview:self.addressLabel];
    [self.bgView addSubview:self.selectButton];
    [self.bgView addSubview:self.editButton];
    [self.bgView addSubview:self.deleteButton];
}
#pragma mark - 二 lazy
- (UIView *)bgView {
    if (_bgView == nil) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UILabel *)nameLabel {
    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc] init];
    }
    return _nameLabel;
}

- (UILabel *)phoneLabel {
    if (_phoneLabel == nil) {
        _phoneLabel = [[UILabel alloc] init];
        _phoneLabel.font = [UIFont systemFontOfSize:13.0];
    }
    return _phoneLabel;
}
- (UILabel *)addressLabel {
    if (_addressLabel == nil) {
        _addressLabel = [[UILabel alloc] init];
        _addressLabel.textColor = XJCOLOR_RGB(110, 110, 110);
        _addressLabel.font = [UIFont systemFontOfSize:14.0];
        _addressLabel.numberOfLines = 0; //希望高度自适应
    }
    return _addressLabel;
}

- (UIButton *)selectButton {
    if (_selectButton == nil) {
        _selectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_selectButton setTitle:@"默认地址" forState:(UIControlStateNormal)];
        _selectButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_selectButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_selectButton setImage:[UIImage imageNamed:@"selected_on"] forState:(UIControlStateSelected)];
        
        [_selectButton setImage:[UIImage imageNamed:@"selected_off"] forState:(UIControlStateNormal)];
        
        [_selectButton addTarget:self action:@selector(selectButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _selectButton;
}

- (UIButton *)editButton {
    if (!_editButton) {
        _editButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_editButton setTitle:@"编辑" forState:(UIControlStateNormal)];
        [_editButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _editButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_editButton setImage:[UIImage imageNamed:@"address_edit"] forState:(UIControlStateNormal)];
        [_editButton addTarget:self action:@selector(editButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _editButton;
}

- (UIButton *)deleteButton{
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_deleteButton setTitle:@"删除" forState:(UIControlStateNormal)];
        [_deleteButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _deleteButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_deleteButton setImage:[UIImage imageNamed:@"address_delt"] forState:(UIControlStateNormal)];
        [_deleteButton addTarget:self action:@selector(deleteButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _deleteButton;
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat kMargin = 15;
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    // 使用Masonry写成死的了 50,30
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(kMargin);
        make.top.equalTo(self.bgView.mas_top).offset(kMargin);
        make.size.mas_equalTo(CGSizeMake(50,30));
    }];
    
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right);
        make.centerY.equalTo(self.nameLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.left.equalTo(self.nameLabel.mas_left);
        make.right.equalTo(self.bgView.mas_right).offset(-kMargin); // 不给高:高度自适应
    }];
    
    [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_left);
        make.top.equalTo(self.addressLabel.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView.mas_right).offset(-kMargin);
        make.top.equalTo(self.addressLabel.mas_bottom).offset((10));
        make.size.mas_equalTo(CGSizeMake((50), (30)));
    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.addressLabel.mas_bottom).offset((10));
        make.right.equalTo(self.deleteButton.mas_left).offset(-10);
        make.size.mas_equalTo(CGSizeMake((50), (30)));
    }];
    
}
#pragma mark - 四 点击/回调事件
- (void)selectButtonAction{
    XJLog(@"默认收货地址");
    self.selectButton.selected = !self.selectButton.isSelected;
    
    if (self.addressCellSelectButtonBlock) {
        self.addressCellSelectButtonBlock(self.selectButton.selected);
    }
}
- (void)editButtonAction{
//    XJLog(@"编辑");
    if (self.addressCellEditButtonBlock) {
        self.addressCellEditButtonBlock();
    }
}
- (void)deleteButtonAction{
//    XJLog(@"删除");
    if (self.addressCellDeleteButtonBlock) {
        self.addressCellDeleteButtonBlock();
    }
}

- (void)configureAddressCellWithName:(NSString *)name phone:(NSString *)phone address:(NSString *)address isDefault:(BOOL)idDefault {
    self.nameLabel.text = name;
    self.phoneLabel.text = phone;
    self.addressLabel.text = address;
    
    self.selectButton.selected = idDefault;
}

@end
